const moment = require("moment");
const db = require("./db");

function Product(pro) {
    this.id = pro.id;
    this.proName = pro.proName;
    this.oldPrice = pro.oldPrice;
    this.price = pro.price;
    this.proDesc = pro.proDesc;
    this.size = pro.size;
    this.statu = pro.statu;
    this.updateTime = pro.updateTime;
    this.num = pro.num;
    this.brandId = pro.brandId;
}

Product.queryProduct = (product, page, callback) => {
    var selectSql = "SELECT * FROM product WHERE statu=1";
    var param = new Array();
    if (product.proName) {
        selectSql = selectSql + " AND  proName LIKE ? ";
        param[0] = '%' + product.proName + '%';
    }
    if (product.brandId) {
        selectSql = selectSql + " AND  brandId =? ";
        param[param.length] = product.brandId;
    }
    if (product.price) {
        if (product.price == 1) selectSql = selectSql + " ORDER BY price ";
        if (product.price == 2) selectSql = selectSql + " ORDER BY price DESC ";
    } else if (product.num) {
        if (product.num == 1) selectSql = selectSql + " ORDER BY num ";
        if (product.num == 2) selectSql = selectSql + " ORDER BY num DESC ";
    }
    selectSql = selectSql + " LIMIT ?,?";
    param[param.length] = (page.page - 1) * page.size;
    param[param.length] = page.size;
    console.log(selectSql);
    console.log(param);
    db.query(selectSql, param, (err, result) => {
        if (err) {
            return callback(err);
        }
        var data = result;
        callback(err, data);
    });
};
Product.countProduct = (callback) => {
    let selectSql = "SELECT count(id) as count FROM product WHERE statu = 1";
    db.query(selectSql, (err, result) => {
        if (err) {
            return callback(err);
        }
        let data = result[0];

        callback(err, data);
    })
}
Product.queryProductDetail = (id, callback) => {
    let selectSql = 'select * from product where id = ?';
    db.query(selectSql, [id], (err, result) => {
        if (err) return callback(err);
        let data = result[0];
        callback(err, data);
    });
}
Product.queryProductDetailList = (product, page, callback) => {
    let selectSql = "SELECT * FROM product WHERE 1=1 ";
    let param = new Array();
    if (product.proName) {
        selectSql = selectSql + " AND proName LIKE ? ";
        param[0] = `%${proName}%`;
    }
    if (product.brandId) {
        selectSql = selectSql + " AND brandId = ? ";
        param[param.length] = product.brandId;
    }
    if (product.price) {
        if (product.price == 1) selectSql = selectSql + " ORDER BY price ";
        if (product.price == 2) selectSql = selectSql + " ORDER BY price DESC ";
    } else if (product.num) {
        if (product.num == 1) selectSql = selectSql + " ORDER BY num";
        if (product.num == 2) selectSql = selectSql + " ORDER BY num DESC";
    } else {
        selectSql = selectSql + "ORDER BY id DESC";
    }
    selectSql = selectSql + " LIMIT ?,?";
    param[param.length] = (page.page - 1) * page.size;
    param[param.length] = page.size;
    console.log(`selectSql=>${selectSql}`);
    console.log(`param=>${param}`);
    db.query(selectSql, param, (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    });
}
Product.countProductDetailList = (callback) => {
    let selectSql = "SELECT count(id) as count FROM product WHERE 1=1";
    db.query(selectSql, (err, result) => {
        if (err) callback(err);
        let data = result[0];
        callback(err, data);
    })
}
Product.addProduct = (product, callback) => {
    let selectSql = 'insert into product (id, proName, oldPrice, price, proDesc, size, statu, updateTime, num, brandId)  values (null,?,?,?,?,?,?,?,?,?)';
    db.query(selectSql, [product.proName, product.oldPrice, product.price, product.proDesc, product.size, product.statu, product.updateTime, product.num, product.brandId], (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    })
}
Product.updateProduct = (product, callback) => {
    let selectSql = 'UPDATE product SET ';
    let param = new Array();
    if (product.proName) {
        selectSql += ' proName=? ';
        param[param.length] = product.proName;
    }
    if (product.oldPrice && param.length == 0) {
        selectSql += ' oldPrice=? ';
        param[param.length] = product.oldPrice;
    } else if (product.oldPrice && param.length != 0) {
        selectSql += ' ,oldPrice=? ';
        param[param.length] = product.oldPrice;
    }
    if (product.price && param.length == 0) {
        selectSql += ' price=? ';
        param[param.length] = product.price;
    } else if (product.price && param.length != 0) {
        selectSql += ' ,price=? ';
        param[param.length] = product.price;
    }
    if (product.proDesc && param.length == 0) {
        selectSql += ' proDesc=? ';
        param[param.length] = product.proDesc;
    } else if (product.proDesc && param.length != 0) {
        selectSql += ' ,proDesc=? ';
        param[param.length] = product.proDesc;
    }
    if (product.size && param.length == 0) {
        selectSql += ' size=? ';
        param[param.length] = product.size;
    } else if (product.size && param.length != 0) {
        selectSql += ' ,size=? ';
        param[param.length] = product.size;
    }
    if (product.statu && param.length == 0) {
        selectSql += ' statu=? ';
        param[param.length] = product.statu;
    } else if (product.statu && param.length != 0) {
        selectSql += ' ,statu=? ';
        param[param.length] = product.statu;
    }
    if (product.num && param.length == 0) {
        selectSql += ' num=? ';
        param[param.length] = product.num;
    } else if (product.num && param.length != 0) {
        selectSql += ' ,num=? ';
        param[param.length] = product.num;
    }
    if (product.brandId && param.length == 0) {
        selectSql += ' brandId=? ';
        param[param.length] = product.brandId;
    } else if (product.brandId && param.length != 0) {
        selectSql += ' ,brandId=? ';
        param[param.length] = product.brandId;
    }

    selectSql += ",updateTime='" + moment().format("YYYY-MM-DD HH:mm:ss") + "'";
    selectSql += " where id=?";
    param[param.length] = product.id;
    db.query(selectSql, param, (err, result) => {
        if (err) return callback(err);
        callback(err, result);
    });
}

module.exports = Product;